//index.js
//获取应用实例
const $vm = getApp()

const cache = Object.create(null)

const {decodeHtml, parseNews} = $vm.utils
var url="http://localhost:8080/CategoryApi/queryAPI";
var pageNum =1;
var page_size = 2;
var totalPage =2;
var id=1;
// 获取数据的方法，具体怎么获取列表数据大家自行发挥
var GetList = function(that){
  that.setData({
    hidden:false
  });

  //栏目文章
  wx.getUserInfo({
      success: function (res) {
          var userInfo = res.userInfo;
          wx.request({
              url: url,
              data: {
                  pageNum: pageNum,
                  page_size: page_size,
                  id: id,
                  userInfo: userInfo
              },
              success: function (res) { 
                  var lists = that.data.list;
                  //var Articlelist = res.data.Articlelist;
                  //var list = that.data.Articlelist.list;
                  var listh = res.data.Articlelist.list.length;
                  for (var i = 0; i <listh; i++) {
                      lists.push(res.data.Articlelist.list[i]);
                  }
                  that.setData({
                      list: lists,
                      bannerlist: res.data.bannerlist,
                      querylist:res.data.querylist,
                      totalPage:res.data.Articlelist.totalPage
                  });
                  that.setData({
                      hidden: true
                  });
              }
          });
      }
  })

}

Page({
    _isLoading: true,
    data: {
        swiperList: [],
        articles: [],
        categoryTabs: [],
        currentTab: 1,
        userInfo: {},
        querylist: {},//栏目
        bannerlist: {},  //轮播图
        articlelist: {},//文章
        hidden:true,
        list:[],
        scrollTop : 0,
        scrollHeight:0
        
    },
   onShow:function(){

  //  在页面展示之后先获取一次数据
  var that = this;
  GetList(that);
 },
    onLoad:function(){
  //  这里要非常注意，微信的scroll-view必须要设置高度才能监听滚动事件，所以，需要在页面的onLoad事件中给scroll-view的高度赋值
   var that = this;
   console.info(that);
   wx.getSystemInfo({
     success:function(res){
       console.info(res.windowHeight);
       that.setData({
         scrollHeight:res.windowHeight
       });
     }
   });
 },
    onReady() {
        this.getNewsList()
    },

    // 页面相关事件处理函数--监听用户下拉动作，下拉刷新
    onPullDownRefresh() {
        // 刷新页面，清空当前的缓存，重新获取
        var chid = this.data.currentTab
        // 命中缓存
        if (cache[chid]) {
            cache[chid] = { slides: [], news: [], page: 0, time: Date.now() }
        }
        this.getNewsList(chid)
    },
    // 切换当前选择的分类
    changeCategory(event) {
        var chid = event.target.dataset.id
        pageNum=1;
        this.setData({ 
        currentTab: chid,
        list:[]
        });
        id=chid;
        var that = this;
        if(pageNum<=totalPage){
        GetList(that);
        }
    },
    tap: function (e) {
        for (var i = 0; i < order.length; ++i) {
            if (order[i] === this.data.toView) {
                this.setData({
                    toView: order[i + 1]
                })
                break
            }
        }
    }, tapMove: function (e) {
        this.setData({
            scrollTop: this.data.scrollTop + 10
        })
    },
    getNewsList(chid = 0, page = 0) {

        if (!cache[chid]) {
            // 新内容
            cache[chid] = { slides: [], news: [], page: 0, time: Date.now() }
        }
        var infos = cache[chid]
        // 获取下一页数据
        if (page) {
            // 加载中。无法触发
            if (this._isLoading) {
                return false;
            }
            infos.page += 1
        } else {
            // 直接从缓存中取出
            if (infos.news.length) {
                this.setData({
                    swiperList: infos.slides,
                    articles0: infos.news
                })
                return false
            }
        }

        this._isLoading = true
    },
    bindDownLoad:function(){
    //  该方法绑定了页面滑动到底部的事件
        var that = this;
        if(pageNum<=totalPage){
        GetList(that);
        pageNum++;
        }
    },
     refresh:function(event){
  //  该方法绑定了页面滑动到顶部的事件，然后做上拉刷新
   pageNum = 1;
   this.setData({
     list : [],
     scrollTop : 0
   });
   GetList(this)
 },
    scroll:function(event){
    //  该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。
    this.setData({
        scrollTop : event.detail.scrollTop
    });
    },
    
    manageTabs() {
        wx.navigateTo({
            url: '/pages/news/manage'
        })
    }
})